package com.jrtc.backend.controller;

import com.jrtc.base.config.dto.ResultDTO;
import com.jrtc.base.config.dto.ResultDTOBuilder;
import com.jrtc.base.entity.bo.CatchBallRecord;
import com.jrtc.base.entity.bo.Doctor;
import com.jrtc.base.entity.bo.DoctorRemark;
import com.jrtc.base.entity.vo.CatchBallRecordVo;
import com.jrtc.service.CatchBallRecordService;
import com.jrtc.service.DoctorRemarkService;
import com.jrtc.service.DoctorService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("doctorRemark")
public class DoctorRemarkController {
    /**
     * 服务对象
     */
    @Resource
    private DoctorRemarkService doctorRemarkService;
    @Resource
    private DoctorService doctorService;

    /**
     * 医生备注列表
     * */
    @PostMapping("/doctorRemarkList")
    public ResultDTO doctorRemarkList(HttpServletRequest request, @RequestBody Map<String, Object> map){
        Integer id = Integer.parseInt(map.get("id").toString());
        Doctor loginDoctor;
        try {
            loginDoctor = doctorService.getLoginDoctor(request);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        if (loginDoctor == null){
            return ResultDTOBuilder.failureMsg("登录失败");
        }

        List<DoctorRemark> doctorRemarkList = doctorRemarkService.doctorRemarkList(loginDoctor.getDoctorId(), id);
        return ResultDTOBuilder.success(doctorRemarkList);
    }

    /**
     * 新增医生备注
     * */
    @PostMapping("/insertRemark")
    public ResultDTO insertRemark(HttpServletRequest request, @RequestBody Map<String, Object> map){
        Doctor loginDoctor;
        try {
            loginDoctor = doctorService.getLoginDoctor(request);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        if (loginDoctor == null){
            return ResultDTOBuilder.failureMsg("登录失败");
        }

        String remark = map.get("remark").toString();
        String memo = map.get("memo").toString();
        Integer patientId = Integer.valueOf(map.get("patientId").toString());

        int i;
        try {
            i = doctorRemarkService.insertRemark(remark, loginDoctor.getDoctorId(), patientId, memo);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        if (i > 0){
            return ResultDTOBuilder.success("添加成功");
        }
        else {
            return ResultDTOBuilder.failureMsg("添加失败");
        }
    }

    /**
     * 医生修改备注
     * @param doctorRemark
     * @return
     */
    @PostMapping("/updateRemark")
    public ResultDTO updateRemark(@RequestBody DoctorRemark doctorRemark) {
        int updateRemark = doctorRemarkService.updateRemark(doctorRemark);
        if (updateRemark > 0)
            return ResultDTOBuilder.success("修改成功");
        else
            return ResultDTOBuilder.failureMsg("修改失败");
    }

    @PostMapping("/queryRemarkById")
    public ResultDTO queryRemarkById(@RequestBody Map<String, Integer> map) {
        Integer id = map.get("id");
        return ResultDTOBuilder.success(doctorRemarkService.queryRemarkById(id));
    }
}